<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ] 
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/3/6-17:04
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\wechat\queue;


    use app\wechat\service\FansService;
    use think\console\Input;
    use think\console\Output;
    use think\Db;
    use app\wechat\service\WechatService;

    /**
     * Class WechatQueue
     * @package app\wechat\queue
     */
    class WechatQueue
    {

        /**
         * 当前类名
         * @var string
         */
        const URI = self::class;

        /**
         * @param Input $input
         * @param Output $output
         * @param array $data
         * @throws \WeChat\Exceptions\InvalidResponseException
         * @throws \WeChat\Exceptions\LocalCacheException
         * @throws \think\Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/9
         * @name: execute
         * @describe:执行任务
         */
        public function execute(Input $input, Output $output, array $data = [])
        {
            $appid = WechatService::getAppid();
            $wechat = WechatService::WeChatUser();
            // 获取远程粉丝
            list($next, $done) = ['', 0];
            while (!is_null($next) && is_array($result = $wechat->getUserList($next)) && !empty($result['data']['openid'])) {
                $done += $result['count'];
                foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
                    if (is_array($list = $wechat->getBatchUserInfo($chunk)) && !empty($list['user_info_list'])) {
                        foreach ($list['user_info_list'] as $user) FansService::set($user, $appid);
                    }
                }
                $next = $result['total'] > $done ? $result['next_openid'] : null;
            }
            // 同步粉丝黑名单
            list($next, $done) = ['', 0];
            while (!is_null($next) && is_array($result = $wechat->getBlackList($next)) && !empty($result['data']['openid'])) {
                $done += $result['count'];
                foreach (array_chunk($result['data']['openid'], 100) as $chunk) {
                    Db::name('WechatFans')->where(['is_black' => '0'])->whereIn('openid', $chunk)->update(['is_black' => '1']);
                }
                $next = $result['total'] > $done ? $result['next_openid'] : null;
            }
            // 同步粉丝标签
            if (is_array($list = WechatService::WeChatTags()->getTags()) && !empty($list['tags'])) {
                foreach ($list['tags'] as &$tag) $tag['appid'] = $appid;
                Db::name('WechatFansTags')->where('1=1')->delete();
                Db::name('WechatFansTags')->insertAll($list['tags']);
            }
        }

    }